package com.lin.missyou.service;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import com.lin.missyou.core.enumeration.OrderStatus;
import com.lin.missyou.exception.http.ServerErrorException;
import com.lin.missyou.model.Order;
import com.lin.missyou.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
public class WxPaymentNotifyService {

    @Autowired
    private WxPaymentService wxPaymentService;

    @Autowired
    private OrderRepository orderRepository;

    /**
     * 将返回的xml转换为Map进行sign校验
     *
     * @param data
     */
    @Transactional
    public void processPayNotify(String data) {
        Map<String, String> dataMap;

        try {
            dataMap = WXPayUtil.xmlToMap(data);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServerErrorException(9999);
        }

        WXPay wxPay = wxPaymentService.assembleWxPayConfig();
        boolean vaild = true;
        try {
            vaild = wxPay.isResponseSignatureValid(dataMap);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("无法校验参数");
            throw new ServerErrorException(9999);
        }
        if (!vaild) {
            System.out.println("参数校验失败");
            throw new ServerErrorException(9999);

        }
        String returnCode = dataMap.get("return_code");
        String resultCode = dataMap.get("result_code");
        String orderNo = dataMap.get("out_trade_no");
        if (!returnCode.equals("SUCCESS")) {
            throw new ServerErrorException(9999);
        }
        if (!resultCode.equals("SUCCESS")) {
            throw new ServerErrorException(9999);
        }
        if (orderNo == null) {
            throw new ServerErrorException(9999);
        }

        //回调消息成功后 unpaid -> paid
        this.deal(orderNo);
    }  /**
     * 将返回的json转换为Map进行sign校验
     *
     * @param data
     */
    @Transactional
    public void processPayNotifyCopy(Map<String,String> data) {

        String returnCode = data.get("return_code");
        String resultCode = data.get("result_code");
        String orderNo =  data.get("out_trade_no");
        if (!returnCode.equals("SUCCESS")) {
            throw new ServerErrorException(9999);
        }
        if (!resultCode.equals("SUCCESS")) {
            throw new ServerErrorException(9999);
        }
        if (orderNo == null) {
            throw new ServerErrorException(9999);
        }

        //回调消息成功后 unpaid -> paid
        this.deal(orderNo);
    }

    private void deal(String orderNo) {
        Order order = orderRepository.findFirstByOrderNo(orderNo)
                .orElseThrow(() -> new ServerErrorException(9999));

        int result = -1;
        if (order.getStatus().equals(OrderStatus.UNPAID.value())
        ||order.getStatus().equals(OrderStatus.CANCELED.value())) {
            result = this.orderRepository.updateOrderStatus(orderNo, OrderStatus.PAID.value());
        }
        if (result != 1) {
            throw new ServerErrorException(9999);
        }

    }
}
